/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ml.dataset;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author TUPEO
 */
public class DatasetHelper {
    List recommendSet, notRecommendSet;
    public DatasetHelper(){
        recommendSet=new ArrayList<>();
        notRecommendSet=new ArrayList<>();
        
    }
    public void deviceDataset(File file, int trainingSetPersontage, File saveFile){
        //reading
        try
        {
            FileInputStream inFile = new FileInputStream(file);
            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook readWorkbook = new XSSFWorkbook(inFile);
 
            //Get first/desired sheet from the workbook
            XSSFSheet readSheet = readWorkbook.getSheetAt(0);
 
            //Iterate through each rows one by one
            Iterator<Row> rowIterator = readSheet.iterator();
            while (rowIterator.hasNext())
            {
                Row row = rowIterator.next();
                if(row.getRowNum()==0) continue;
                Cell cell=row.getCell(13);
                if(cell.getNumericCellValue()==1){
                    recommendSet.add(row.getRowNum());
                }else{
                    notRecommendSet.add(row.getRowNum());
                }
            }
            
            Collections.shuffle(recommendSet);
            Collections.shuffle(notRecommendSet);
            
            //Blank workbook
            XSSFWorkbook writeWorkbook = new XSSFWorkbook();
         
            //Create a blank sheet
            XSSFSheet trainingSheet = writeWorkbook.createSheet("training");
            XSSFSheet validationSheet = writeWorkbook.createSheet("validation");
            XSSFSheet testingSheet = writeWorkbook.createSheet("testing");
            XSSFSheet fullDatasetSheet = writeWorkbook.createSheet("full");
            
            for(int i=0;i<=500;i++){
                Row readRow=readSheet.getRow(i);
                Row row=fullDatasetSheet.createRow(i);
                for(int j=0;j<14;j++){
                    Cell cell=row.createCell(j);
                    Cell readCell=readRow.getCell(j);
                    switch (readCell.getCellType()){
                        case Cell.CELL_TYPE_BLANK: break;
                        case Cell.CELL_TYPE_NUMERIC: cell.setCellValue(readCell.getNumericCellValue());break;
                        case Cell.CELL_TYPE_STRING: cell.setCellValue(readCell.getStringCellValue());break;    
                    }
                    
                }
            }
            
            int trainingSizeRecommend=recommendSet.size()*trainingSetPersontage/100;
            int trainingSizeNotRecommend=notRecommendSet.size()*trainingSetPersontage/100;
            
            int traningRowNum=0, validateRowNum=0, testRowNum=0;
            for(int i=0;i<recommendSet.size();i++){
                Row readRow=readSheet.getRow((int)recommendSet.get(i));
                Row row;
                if(i<trainingSizeRecommend){
                    row = trainingSheet.createRow(traningRowNum++);
                }else if (i<trainingSizeRecommend+(recommendSet.size()-trainingSizeRecommend)/2){
                    row = validationSheet.createRow(validateRowNum++);
                }else{
                    row =testingSheet.createRow(testRowNum++);
                }
                for(int j=0;j<14;j++){
                    Cell cell=row.createCell(j);
                    Cell readCell=readRow.getCell(j);
                    switch (readCell.getCellType()){
                        case Cell.CELL_TYPE_BLANK: break;
                        case Cell.CELL_TYPE_NUMERIC: cell.setCellValue(readCell.getNumericCellValue());break;
                        case Cell.CELL_TYPE_STRING: cell.setCellValue(readCell.getStringCellValue());break;    
                    }
                    
                }
            }
            
            for(int i=0;i<notRecommendSet.size();i++){
                Row readRow=readSheet.getRow((int)notRecommendSet.get(i));
                Row row;
                if(i<trainingSizeNotRecommend){
                    row = trainingSheet.createRow(traningRowNum++);
                }else if (i<trainingSizeNotRecommend+(notRecommendSet.size()-trainingSizeNotRecommend)/2){
                    row = validationSheet.createRow(validateRowNum++);
                }else{
                    row =testingSheet.createRow(testRowNum++);
                }
                for(int j=0;j<14;j++){
                    Cell cell=row.createCell(j);
                    Cell readCell=readRow.getCell(j);
                    switch (readCell.getCellType()){
                        case Cell.CELL_TYPE_BLANK: break;
                        case Cell.CELL_TYPE_NUMERIC: cell.setCellValue(readCell.getNumericCellValue());break;
                        case Cell.CELL_TYPE_STRING: cell.setCellValue(readCell.getStringCellValue());break;    
                    }
                    
                }
            }
            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(saveFile);
            writeWorkbook.write(out);
            out.close();
            inFile.close();
        }
        catch (IOException e)
        {
            System.out.println(e);
        }
    }
    
}
